﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLDBHYT.DAO;
using QLDBHYT.VO;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace QLDBHYT.BUS
{
    class P2hBUS
    {
        private P2hDAO p2hDAO;
        public P2hBUS()
        {
            p2hDAO = new P2hDAO();
        }
        public P2hVO GetMathe(string mathe)
        {

            P2hVO p2hVO = new P2hVO();
            DataTable dataTable = new DataTable();
            dataTable = p2hDAO.SearhMathe(mathe);
            foreach (DataRow dr in dataTable.Rows)
            {
                p2hVO.Hoten = dr["ho_ten"].ToString();
                p2hVO.Gioitinh = Convert.ToInt16(dr["gioi_tinh"]);
                p2hVO.Ngaysinh = Convert.ToInt16(dr["Ngay_sinh"]);
                p2hVO.Thangsinh = Convert.ToInt16(dr["Thang_sinh"]);
                p2hVO.Namsinh = Convert.ToInt16(dr["nam_sinh"]);
                p2hVO.Diachi  = dr["dia_chi"].ToString();
                p2hVO.Tinhkcb = Convert.ToString(dr["tinh_kcb"]);
                p2hVO.Makcb = dr["ma_kcb"].ToString();
                p2hVO.Noikham  = dr["noi_kham"].ToString();
                p2hVO.Gttu = dr["gtri_tu"].ToString();
                p2hVO.Gtden = dr["gtri_den"].ToString();
                p2hVO.Benhvien = dr["noi_kham"].ToString();
              
            }
            return p2hVO;
        }
        public bool CheckMaTheTheoNgay(string ngay, string mathe,string sophieu)
        {
            P2hVO p2dVO = new P2hVO();
            DataTable dataTable = new DataTable();
            dataTable = p2hDAO.SearchMaTheTheoNgay(ngay, mathe,sophieu);
            if (dataTable.Rows.Count >= 1)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        public List<khoakhamVO> GetKhoaKham()
        {

            List<khoakhamVO> lstP2hVO = new List<khoakhamVO>();
            khoakhamVO P2hVO;
            DataTable dataTable = new DataTable();
            dataTable = p2hDAO.SearchKhoa();
            foreach (DataRow dr in dataTable.Rows)
            {
                P2hVO = new khoakhamVO();
                P2hVO.Makhoa = dr["Ma_khoa"].ToString();
                P2hVO.Tenkhoa = dr["Ten_khoa"].ToString();
                lstP2hVO.Add(P2hVO);
            }
            return lstP2hVO;
        }
        public List<DoituongVO> GetDoiTuong()
        {

            List<DoituongVO> lstDoituongVO = new List<DoituongVO>();
            DoituongVO doituongVO;
            DataTable dataTable = new DataTable();
            dataTable = p2hDAO.SearchDoiTuong();
            foreach (DataRow dr in dataTable.Rows)
            {
                doituongVO = new DoituongVO();
                doituongVO.Madt = dr["ma_dt"].ToString();
                doituongVO.Loaitt = dr["loai_tt"].ToString();
                doituongVO.Vanchuyen = dr["vanchuyen"].ToString();
                lstDoituongVO.Add(doituongVO);
            }
            return lstDoituongVO;
        }
        public List<ThuocVO> GetTenHieu(int thang,int nam)
        {

            List<ThuocVO> lstThuocVO = new List<ThuocVO>();
            ThuocVO thuocVO;
            DataTable dataTable = new DataTable();
            dataTable = p2hDAO.SearchTenHieu(thang,nam);
            foreach (DataRow dr in dataTable.Rows)
            {
                if (!( Convert.ToInt32(dr["slcuoiky"]) == 0 && (dr["stt_nh"].ToString().Trim() == "01"
                    || dr["stt_nh"].ToString().Trim() == "03") ))
                {
                    thuocVO = new ThuocVO();
                    thuocVO.Sttnh  = dr["stt_nh"].ToString().Trim();
                    thuocVO.Mahieu = dr["Ma_hieu"].ToString().Trim();
                    thuocVO.Tenhieu = dr["ten_hieu"].ToString().Trim();
                    thuocVO.Dvt = dr["dvt"].ToString();
                    thuocVO.Gia  = Convert.ToDecimal(dr["gia"]);
                    thuocVO.Soluong = Convert.ToInt32(dr["slcuoiky"]);
                    thuocVO.Giabhxh = Convert.ToDecimal(dr["gia_bhxh"]);
                    thuocVO.Ktc = dr["ktc"].ToString();
                    lstThuocVO.Add(thuocVO);
                }
            }
            return lstThuocVO;
        }
        public P2hVO InsertRecord(P2hVO p2hVO, List<P2duVO> lstP2duVO, List<P2dVO> lstP2dVO)
        {
            return p2hDAO.InsertRecord(p2hVO, lstP2duVO, lstP2dVO);
        }
        public P2hVO UpdateRecord(P2hVO p2hVO, List<P2duVO> lstP2duVO, List<P2dVO> lstP2dVO)
        {
            return p2hDAO.UpdateRecord(p2hVO, lstP2duVO, lstP2dVO);
        }
        public bool DeleteRecord(string ctid)
        {
            return p2hDAO.DeleteRecord(ctid);
        }
        public bool UpdateRecodrP2D_Detail(List<P2dVO> lstP2dVO)
        {
            return p2hDAO.UpdateRecodrP2D_Detail(lstP2dVO);
        }
        public List<P2hVO> GetListP2H(string table,string condition)
        {
            List<P2hVO> lstP2H = new List<P2hVO>();
            P2hVO p2hVO;
            DataTable dataTable = new DataTable();
            dataTable = p2hDAO.Search(table,condition);
            if (dataTable == null) { return null; }
            foreach (DataRow dr in dataTable.Rows)
            {
                p2hVO = new  P2hVO();
                GetP2H(p2hVO, dr);
                lstP2H.Add(p2hVO);

            }
            return lstP2H;
        }
        private void GetP2H(P2hVO p2hVO, DataRow dr)
        {
            p2hVO.Ctid = dr["ct_id"].ToString();
            p2hVO.Loai = dr["loai"].ToString();
            p2hVO.Matinh = dr["matinh"].ToString();
            p2hVO.Sophieu = dr["sophieu"].ToString();
            p2hVO.Benhvien = dr["benh_vien"].ToString();
            p2hVO.Makhoa = Convert.ToString(dr["ma_khoa"]);
            p2hVO.Tenkhoa = dr["ten_khoa"].ToString();
            p2hVO.Mathe = dr["mathe"].ToString();
            p2hVO.Madt = dr["ma_dt"].ToString();
            p2hVO.Loaitt = Convert.ToString(dr["loai_tt"]);
            p2hVO.Vanchuyen = Convert.ToString(dr["vanchuyen"]);
            p2hVO.Loaitt1 = Convert.ToDecimal(dr["loai_tt1"]);
            p2hVO.Hoten = Convert.ToString(dr["ho_ten"]);
            p2hVO.Gioitinh = Convert.ToInt16(dr["gioi_tinh"]);
            p2hVO.Ngaysinh = Convert.ToInt32(dr["Ngay_sinh"]);
            p2hVO.Thangsinh = Convert.ToInt32(dr["Thang_sinh"]);
            p2hVO.Namsinh = Convert.ToInt32(dr["nam_sinh"]);
            p2hVO.Diachi = Convert.ToString(dr["dia_chi"]);
            p2hVO.Tinhkcb  = Convert.ToString(dr["tinh_kcb"]);
            p2hVO.Makcb = Convert.ToString(dr["ma_kcb"]);
            p2hVO.Noikham = Convert.ToString(dr["noi_kham"]);
            p2hVO.Ngayvao = Convert.ToString(dr["ngay_vao"]);
            p2hVO.Ngayra = Convert.ToString(dr["ngay_ra"]);
            p2hVO.Gttu = Convert.ToString(dr["gtri_tu"]);
            p2hVO.Gtden = Convert.ToString(dr["gtri_den"]);
            p2hVO.Ngaytt = Convert.ToString(dr["ngay_tt"]);
            p2hVO.Maicd = Convert.ToString(dr["ma_icd"]);
            p2hVO.Tenbenh = Convert.ToString(dr["ten_benh"]);
            p2hVO.Benhan = Convert.ToString(dr["benhan"]);
            p2hVO.Mabhxh = Convert.ToString(dr["ma_bhxh"]);
            p2hVO.Lenhdx = Convert.ToString(dr["lenh_dx"]);
            p2hVO.Noiden = Convert.ToString(dr["noi_den"]);
            p2hVO.Ngay1 = Convert.ToString(dr["ngay_g1"]);
            p2hVO.Ngay2 = Convert.ToString(dr["ngay_g2"]);
            p2hVO.Ngay3 = Convert.ToString(dr["ngay_g3"]);
            p2hVO.Ngaylv = Convert.ToString(dr["ngay_lv"]);
            p2hVO.Usernam = Convert.ToString(dr["user_name"]);
            p2hVO.Excel = Convert.ToInt32(dr["excel"]);
            p2hVO.Doituong = Convert.ToInt32(dr["doituong"]);
            p2hVO.Dieutri = Convert.ToString(dr["dieu_tri"]);
        }
        public List<P2duVO> GetListP2DU(string table, string ctid)
        {
            List<P2duVO> lstP2du = new List<P2duVO>();
            P2duVO p2duVO;
            DataTable dataTable = new DataTable();
            dataTable = p2hDAO.SearchP2DU(table, ctid);
            if (dataTable == null) { return null; }
            foreach (DataRow dr in dataTable.Rows)
            {
                p2duVO = new P2duVO();
                p2duVO.Ctid = ctid;
                p2duVO.Ctid2 = dr["ct_id2"].ToString();
                p2duVO.Sttnh = dr["stt_nh"].ToString();
                p2duVO.Tongtien = Convert.ToDecimal(dr["tong_tien"]);
                p2duVO.Tienbn = Convert.ToDecimal(dr["tien_bn"]);
                lstP2du.Add(p2duVO);

            }
            return lstP2du;
        }
        public List<P2dVO> GetListP2D(string table, string ctid)
        {
            List<P2dVO> lstP2d = new List<P2dVO>();
            P2dVO p2dVO;
            DataTable dataTable = new DataTable();
            dataTable = p2hDAO.SearchP2D(table, ctid);
            if (dataTable == null) { return null; }
            foreach (DataRow dr in dataTable.Rows)
            {
                p2dVO = new P2dVO();
                p2dVO.Ctid = ctid;
                p2dVO.Ctid0 = dr["ct_id0"].ToString();
                p2dVO.Sttnh = dr["stt_nh"].ToString();
                p2dVO.Mahieu = dr["Ma_hieu"].ToString();
                p2dVO.Tenhieu = dr["ten_hieu"].ToString();
                p2dVO.Tyle = 100;
                p2dVO.Ktc = dr["ktc"].ToString();
                p2dVO.Sl = Convert.ToInt16(dr["sl"]);
                p2dVO.Gia = Convert.ToDecimal(dr["gia"]);
                p2dVO.Giabhxh = Convert.ToDecimal(dr["gia_bhxh"]);
                p2dVO.Tongtien = Convert.ToDecimal(dr["tong_tien"]);
                p2dVO.Tienbhxh = Convert.ToDecimal(dr["tien_bhxh"]);
                p2dVO.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                p2dVO.Tien_bn = Convert.ToDecimal(dr["tien_bn"]);
                p2dVO.Dvt = dr["dvt"].ToString();
                p2dVO.Loai = 1;
                p2dVO.Bacsy = "";
                p2dVO.Khoa = "";
                p2dVO.Chon = 0;
                p2dVO.Flag = 0;
                lstP2d.Add(p2dVO);

            }
            return lstP2d;
        }
        public DataTable GetBV(string BV)
        {
            return p2hDAO.SearchBV(BV);
        }
        public List<P2dVO> GetListP2d_Detail(string ctid,DateTime ngay)
        {
            List<P2dVO> lstP2d = new List<P2dVO>();
            P2dVO p2dVO;
            DataTable dataTable = new DataTable();
            dataTable = p2hDAO.SearchP2d_Detail(ctid,ngay);
            if (dataTable == null) { return null; }
            foreach (DataRow dr in dataTable.Rows)
            {
                p2dVO = new P2dVO();
                p2dVO.Ctid = ctid;
                p2dVO.Ctid0 = dr["ct_id0"].ToString();
                p2dVO.Sttnh = dr["stt_nh"].ToString();
                p2dVO.Mahieu = dr["Ma_hieu"].ToString();
                p2dVO.Tenhieu = dr["ten_hieu"].ToString();
                p2dVO.Tyle = 100;
                p2dVO.Ktc = dr["ktc"].ToString();
                p2dVO.Sl = Convert.ToInt16(dr["sl"]);
                p2dVO.Gia = Convert.ToDecimal(dr["gia"]);
                p2dVO.Giabhxh = Convert.ToDecimal(dr["gia_bhxh"]);
                p2dVO.Tongtien = Convert.ToDecimal(dr["tong_tien"]);
                p2dVO.Tienbhxh = Convert.ToDecimal(dr["tien_bhxh"]);
                p2dVO.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                p2dVO.Tien_bn = Convert.ToDecimal(dr["tien_bn"]);
                p2dVO.Dvt = dr["dvt"].ToString();
                p2dVO.Ngay = dr["Ngay"].ToString();
                p2dVO.Loai = 1;
                p2dVO.Bacsy = "";
                p2dVO.Khoa = "";
                p2dVO.Chon = 0;
                p2dVO.Flag = 0;
                lstP2d.Add(p2dVO);
            }
            return lstP2d;
        }
        public DataTable GetListP2d_Detail(string ctid)
        {
            return p2hDAO.SearchP2d_Detail(ctid);
        }
        public List<P2dVO> GetListP2D_Detail(string ctid)
        {
            List<P2dVO> lstP2d = new List<P2dVO>();
            P2dVO p2dVO;
            DataTable dataTable = new DataTable();
            dataTable = p2hDAO.SearchP2d_Detail_Sum(ctid);
            if (dataTable == null) { return null; }
            foreach (DataRow dr in dataTable.Rows)
            {
                p2dVO = new P2dVO();
                p2dVO.Ctid = ctid;
                p2dVO.Sttnh = dr["stt_nh"].ToString();
                p2dVO.Mahieu = dr["Ma_hieu"].ToString();
                p2dVO.Tenhieu = dr["ten_hieu"].ToString();
                p2dVO.Tyle = 100;
                p2dVO.Ktc = dr["ktc"].ToString();
                p2dVO.Sl = Convert.ToInt16(dr["sl"]);
                p2dVO.Gia = Convert.ToDecimal(dr["gia"]);
                p2dVO.Giabhxh = Convert.ToDecimal(dr["gia_bhxh"]);
                p2dVO.Tongtien = p2dVO.Sl * p2dVO.Gia;
                p2dVO.Tienbhxh = p2dVO.Sl * p2dVO.Giabhxh;
                p2dVO.Dvt = dr["dvt"].ToString();
                p2dVO.Loai = 1;
                p2dVO.Bacsy = "";
                p2dVO.Khoa = "";
                p2dVO.Chon = 0;
                p2dVO.Flag = 0;
                lstP2d.Add(p2dVO);

            }
            return lstP2d;
        }
        public bool DeleteRecordP2d_Detail(string ctid,DateTime ngay)
        {
            return p2hDAO.DeleteRecordP2D_Detail(ctid, ngay);
        }

    }
}
